package com.jzy.gulimall.ware.controller;

import com.jzy.common.utils.PageUtils;
import com.jzy.common.utils.R;
import com.jzy.gulimall.ware.entity.PurchaseEntity;
import com.jzy.gulimall.ware.service.PurchaseService;
import com.jzy.gulimall.ware.vo.MergeVo;
import com.jzy.gulimall.ware.vo.PurchaseDoneVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 采购信息
 *
 * @author jzy
 * @email 1802511725@qq.com
 * @date 2020-07-12 08:58:54
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 完成采购单
     * ware/purchase/done
     * {
     *    "id": 3,
     *    "items": [{"itemId":1,"status":4,"reason":""}]
     * }
     */
    @PostMapping("/done")
    public R done(@RequestBody PurchaseDoneVo doneVo){
        purchaseService.donePurchase(doneVo);
        return R.ok();
    }

    /**
     * 领取采购单
     * http://localhost:88/api/ware/purchase/received
     * [1,2,3,4] json
     */
    @PostMapping("/received")
    public R received(@RequestBody List<Long> ids){
        purchaseService.receivedPurchase(ids);
        return R.ok();
    }

    /**
     * 采购单维护 采购需求 合并整单 合并
     * ware/purchase/merge  {"purchaseId":2,"items":[1,2]}
     */
    @RequestMapping("/merge")
    public R merge(@RequestBody MergeVo mergeVo){
        purchaseService.mergePurchase(mergeVo);
        return R.ok();
    }

    /**
     * 采购单维护 采购需求 合并到整单 采购单列表
     * ware/purchase/unreceive/list
     */
    @RequestMapping("/unreceive/list")
    public R unreceiveList(){
        List<PurchaseEntity> list = purchaseService.unreceiveList();
        return R.ok().put("list", list);
    }

    /**
     * 采购单维护 采购单 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ware:purchase:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ware:purchase:info")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 采购单维护 采购单 新增
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase){
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
		purchaseService.save(purchase);
        return R.ok();
    }

    /**
     * 采购单维护 采购单 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ware:purchase:update")
    public R update(@RequestBody PurchaseEntity purchase){
        purchase.setUpdateTime(new Date());
		purchaseService.updateById(purchase);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ware:purchase:delete")
    public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
